#include "xmlimport.h"

XmlImport::XmlImport(QString prodLocation, QString suppLocation)
{
    QFile file(prodLocation);
    if(!file.open(QIODevice::ReadOnly |QIODevice::Text))
    {
        qDebug()<<"Failed to open products file";
    }
    else
    {
        if(!documentProd.setContent(&file))
        {
            qDebug()<<"Failed to load products document";
        }
        file.close();
    }

    QFile file2(suppLocation);
    if(!file.open(QIODevice::ReadOnly |QIODevice::Text))
    {
        qDebug()<<"Failed to open suppliers file";
    }
    else
    {
        if(!documentSupp.setContent(&file2))
        {
            qDebug()<<"Failed to load suppliers document";
        }
        file2.close();
    }
}

Product XmlImport::getMainProduct(QVector <Product> productsList){

    if(documentProd.isNull())
    {
        qDebug()<<"Content not set";
        Product pusty("","");
        return pusty;
    }
    QDomElement root = documentProd.firstChildElement();
    QDomElement mainProduct = root.elementsByTagName("mainproduct").at(0).toElement();
    QMap <int,float> semiProductsList;
    QDomNodeList itemList = mainProduct.elementsByTagName("semiproduct");
    for(int i=0;i<itemList.count();i++)
    {
        QDomNode item = itemList.at(i);
        if(item.isElement())
        {
            QDomElement itemEle = item.toElement();
            int id = Product::getProductId( itemEle.attribute("name"), productsList);
            //qDebug()<<"Poprosilem o: "<<itemEle.attribute("name")<<"dostalem "<<id<<endl;
            float quantity = itemEle.attribute("quantity").toFloat();
            semiProductsList.insert(id,quantity);
        }
    }
    Product result(mainProduct.attribute("name"),semiProductsList,mainProduct.attribute("unit"));
    return result;


}

QVector<Product> XmlImport::getProducts(){
    QVector<Product> result;
    if(documentProd.isNull())
    {
        qDebug()<<"Content not set";

        return result;
    }
    QDomElement root = documentProd.firstChildElement();
    QDomNodeList itemList = root.elementsByTagName("product");
    for(int i=0; i< itemList.count() ; i++){

        QDomNode item = itemList.at(i);
        if(item.isElement()){
            QDomElement itemEle = item.toElement();
            QString name = itemEle.attribute("name");
            QString unit = itemEle.attribute("unit");
            Product toAdd(name,unit);
            result.push_back(toAdd);

        }
    }
return result;
}

Supplier XmlImport::getMainSupplier(Product mainProduct)
{
    if(documentSupp.isNull())
    {
        qDebug()<<"Content not set";
        Supplier empty("",0,0,0,0);
        return empty;
    }
     QDomElement root = documentSupp.firstChildElement();
     QDomElement mainSupplier = root.firstChild().toElement();
     QString name = mainSupplier.attribute("name");
     int productId = mainProduct.getId();
     int dailyProduction = mainSupplier.attribute("dailyProduction").toInt();
     int cost = mainSupplier.attribute("cost").toInt();
     int productionTime = mainSupplier.attribute("productionTime").toInt();
     qDebug()<<name<<productId<<cost<<productionTime<<dailyProduction;
     Supplier result(name,productId,cost,productionTime,dailyProduction);
     return result;
}

QVector<Supplier> XmlImport::getSuppliers(QVector<Product> productsList)
{
    QVector<Supplier> result;
    if(documentSupp.isNull())
    {
        qDebug()<<"Content not set";
        return result;
    }
     QDomElement root = documentSupp.firstChildElement();
     QDomNodeList itemList = root.elementsByTagName("supplier");
     for(int i = 0; i<itemList.count();i++)
     {
         QDomElement itemEle = itemList.at(i).toElement();
         QString name = itemEle.attribute("name");
         int productId = Product::getProductId(itemEle.attribute("product"),productsList);
         int dailyProduction = itemEle.attribute("dailyProduction").toInt();
         float cost = itemEle.attribute("cost").toFloat();
         int productionTime = itemEle.attribute("productionTime").toInt();
         Supplier toAdd(name,productId,cost,productionTime,dailyProduction);
         result.push_back(toAdd);
     }
    return result;
}
